package com.bestcem.xm.label.autolabel;

import com.bestcem.xm.label.constant.Constants;
import com.bestcem.xm.label.grpc.service.param.LabelValueIdParam;
import com.bestcem.xm.label.service.dto.LabelValueDTO;
import com.bestcem.xm.label.service.dto.SurveyConditionDTO;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ming.xue <ming.xue@idiaoyan.com>
 * @desc 自动打标签上下文
 * @date 2021-05-19
 */
@Getter
@Setter
public class LabelMemberContext {
    /**
     * 企业标识
     */
    private String orgId;
    /**
     * 批次号
     */
    private String batchId;
    /**
     * 等待被打标签的联系人标识列表
     */
    private List<String> memberIds;

    /**
     * 标签值层级结构
     * 以层级方式组织的当前企业的所有标签值
     * <labelId, LabelValueDTO[]>
     *
     */
    private Map<String, List<LabelValueDTO>> labelMap;
    /**
     * 问卷相关条件
     * <conditionId, SurveyConditionDTO>
     */
    private Map<String, SurveyConditionDTO> surveyConditionMap =
            new HashMap<>(Constants.SURVEY_CONDITION_INITIAL_CAPACITY);

    /**
     * 问卷相关条件计算结果
     * <conditionId:memberId, match>
     */
    private Map<String, Integer> surveyConditionResult;

    // <memberId, valueIds>
    private Map<String, List<LabelValueIdParam>> modifiedMemberLabelMap;


    /**
     * 层级相关
     */
    private Map<String, List<String>> groupMap;

    /**
     * 经过拓补排序的表现标识列表
     */
    private List<String> sortedLabelIds;

    public void addSurveyCondition(String conditionId, SurveyConditionDTO surveyCondition) {
        this.surveyConditionMap.put(conditionId, surveyCondition);
    }

    /**
     * 建议清理内存
     */
    public void clean() {
        if (!CollectionUtils.isEmpty(memberIds)) {
            memberIds.clear();
            memberIds = null;
        }
        if (!CollectionUtils.isEmpty(labelMap)) {
            labelMap.clear();
            labelMap = null;
        }
        if (!CollectionUtils.isEmpty(surveyConditionMap)) {
            surveyConditionMap.clear();
            surveyConditionMap = null;
        }
        if (!CollectionUtils.isEmpty(surveyConditionResult)) {
            surveyConditionResult.clear();
            surveyConditionResult = null;
        }
        if (!CollectionUtils.isEmpty(modifiedMemberLabelMap)) {
            modifiedMemberLabelMap.clear();
            modifiedMemberLabelMap = null;
        }
    }
}
